package main;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import service.DemoService;
import service.DemoServiceImpl;

import java.io.IOException;

public class ProviderMain {

    public static void main(String[] args) throws IOException {

        DemoService demoService = new DemoServiceImpl();

        // Application Info
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("DubboDemo");

        // Registry Info
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://127.0.0.1:2181");
//        registry.setUsername("aaa");
//        registry.setPassword("bbb");

        // Protocol
        ProtocolConfig protocol = new ProtocolConfig();
        protocol.setName("dubbo");
        protocol.setPort(20890);
        protocol.setThreads(200);

        // NOTES: ServiceConfig为重对象，内部封装了与注册中心的连接，以及开启服务端口

        // Exporting
        ServiceConfig<DemoService> service = new ServiceConfig<>(); // 此实例很重，封装了与注册中心的连接，请自行缓存，否则可能造成内存和连接泄漏
        service.setApplication(applicationConfig);
        service.setRegistry(registry);
        service.setProtocol(protocol);
        service.setInterface(DemoService.class);
        service.setRef(demoService);
        service.setVersion("1.0.0");

        // Local export and register
        service.export();

        System.in.read();
    }

}
